﻿using System;
using System.Xml;
using System.Xml.Serialization;

namespace GISUtil.KML
{
  /// <summary>
  /// Contains methods for serializing and deserializing the XML found in the IconStyle section of KML
  /// </summary>
  [XmlRoot("IconStyle")]
  public class IconStyle
  {
    /// <summary>
    /// The unique ID of this style.
    /// </summary>
    [XmlAttribute("Id")]
    public string ID { get; set; }

    /// <summary>
    /// The Color of the style.
    /// </summary>
    [XmlElement("color")]
    public string Color { get; set; }

    /// <summary>
    /// The Color mode associated with this style.
    /// </summary>
    [XmlIgnore]
    public ColorMode? ColorMode { get; set; }

    [XmlElement("colorMode")]
    public string ColorModeString
    {
      get
      {
        if (ColorMode.HasValue)
        {
          return ColorMode.Value.ToString();
        }
        return null;
      }
      set
      {
        ColorMode mode;
        if (value != null && Enum.TryParse<ColorMode>(value, out mode))
        {
          ColorMode = mode;
        }
        else
        {
          ColorMode = null;
        }
      }
    }
        
    /// <summary>
    /// Used to resize the Icon.
    /// </summary>
    [XmlIgnore]
    public float? Scale { get; set; }

    [XmlElement("scale")]
    public string ScaleString
    {
      get
      {
        if (Scale.HasValue)
        {
          return Scale.Value.ToString();
        }
        return null;
      }
      set
      {
        float scale;
        if (value != null && float.TryParse(value, out scale))
        {
          Scale = scale;
        }
        else
        {
          Scale = null;
        }
      }
    }

    /// <summary>
    /// The direction in degrees. Can range from 0 to 360, where 0 is north. 
    /// </summary>
    [XmlIgnore]
    public float? Heading { get; set; }

    [XmlElement("heading")]
    public string HeadingString
    {
      get
      {
        if (Heading.HasValue)
        {
          return Heading.Value.ToString();
        }
        return null;
      }
      set
      {
        float heading;
        if (value != null && float.TryParse(value, out heading))
        {
          Heading = heading;
        }
        else
        {
          Heading = null;
        }
      }
    }

    /// <summary>
    /// The Icon associated with this style.
    /// </summary>
    [XmlElement("Icon")]
    public KMLIcon Icon { get; set; }

    /// <summary>
    /// The position within the Icon that is anchored to the point that the Icon corresponds to. 
    /// </summary>
    [XmlElement("hotSpot")]
    public HotSpot HotSpot { get; set; }

    /// <summary>
    /// Determines if this class has any styling information associated with it. 
    /// </summary>
    /// <returns>True if at least one of the fields that contains style information has a value, otherwise false.</returns>
    public bool HasAValue()
    {
      return ID != null ||
              Color != null ||
              ColorMode != null ||
              Scale != null ||
              Heading != null ||
              (Icon != null && Icon.HasAValue()) ||
              (HotSpot != null && HotSpot.HasAValue());
    }
  }
}